<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!--    引入jq    -->
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>

</head>
    <body>
        <h3>welcome to index! good!</h3>
        <a href="/login">点击进入</a>
        <br>
<!--        <form method="post" action="/upload" enctype="multipart/form-data">-->
<!--            <input type="file" name="file"><br>-->
<!--            <input type="submit" value="提交">-->
<!--        </form>-->
        <button id="submit">上传用户头像</button>
        <input type="file" name="file" id="file" style="display: none;" onchange="upload()">

        <button id="submitImg">上传图片</button>
        <input type="file" name="file" id="file1" style="display: none;" onchange="uploadImg()">
        <img id="preview_photo" src="" width="200px" height="200px">
    </body>

<script>

    $(document).ready(function(){

        $("#submit").click(function () {
            $("#file").click();
        })

        $("#submitImg").click(function () {
            $("#file1").click();
        })

    })

    function upload(){
        if ($("#photoFile").val() == '') {
            return;
        }
        var formData = new FormData();
        formData.append('file', document.getElementById('file').files[0]);
        formData.append('username','allala');
        $.ajax({
            url: '/user/upload',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(data){
                console.log(data);
            },
            error: function(data) {
                alert("上传失败")
            }
        })
    }

    function uploadImg(){
        if ($("#photoFile").val() == '') {
            return;
        }
        var formData = new FormData();
        formData.append('file', document.getElementById('file1').files[0]);
        formData.append('articleId','6');
        $.ajax({
            url: '/article/upload',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(data){
                console.log(data);
            },
            error: function(data) {
                alert("上传失败")
            }
        })
    }



</script>
</html>